package demo.letcode.top100;

import java.util.ArrayList;
import java.util.List;

/**
 * 给你一个字符串，将是分割成一些字串
 * 使每个字串都是回文串
 *
 * @author taqo
 * @date 2021/5/12
 */
public class _分割回文串 {
    public static void main(String[] args) {
        String s = "aab";
        List<List<String>> list = new ArrayList<>();
        List<String> res = new ArrayList<>();
        def(list,res,s,0);
        System.out.println(list);
    }

    private static void def(List<List<String>> list, List<String> res, String str, int idx) {
        int len = str.length();
        if (idx == len) {
            list.add(new ArrayList<>(res));
            return;
        }

        for (int i = idx; i < len; i++) {
            if(!isPa(str,idx,i)){
                continue;
            }

            res.add(str.substring(idx,i+1));

            def(list,res,str,idx + 1);
            res.remove(res.size() - 1);
        }
    }

    private static boolean isPa(String str, int l, int r) {
        if (str.charAt(l++) != str.charAt(r--)){
            return false;
        }
        return true;
    }
}
